//
//  YWTemplatePuzzleCell.m
//  YWPhotoEditMaster
//
//  Created by jun peng on 2023/8/31.
//

#import "YWTemplatePuzzleCell.h"

@interface YWTemplatePuzzleCell ()///<UIScrollViewDelegate>

//@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIImageView *imageView;



@end

@implementation YWTemplatePuzzleCell

-(void)reloadFrame{
    [self layoutSubviews];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
//    self.scrollView.frame = self.contentView.bounds;
    
    
//它是根据cell的宽高来判断的（4种情况）
    UIImage *image = self.status.image;
    
    if(self.contentView.height > self.contentView.width){ //cell的高大于宽
        
        CGFloat w = self.contentView.width;
        CGFloat h = w * image.size.height * 1.0 / image.size.width;
        CGFloat y = 0;
        CGFloat x = 0;
        if (h > self.contentView.height) { //图片高度大于cell的高度、就要左右贴边、上下突出来
            
            w = self.contentView.width;
            h = w * image.size.height * 1.0 / image.size.width;
            y = 0;
            x = 0;
            
            self.imageView.bounds = CGRectMake(x, y, w, h);
            self.imageView.center = CGPointMake(self.contentView.width*0.5, self.contentView.height*0.5);
            
//            self.scrollView.contentSize = CGSizeMake(w, h);
//
//            self.scrollView.contentInset = UIEdgeInsetsMake(100, 100, 100, 100);
//
//            self.scrollView.contentOffset = CGPointMake(0, (h - self.contentView.height)*0.5);
            
              
            
        }
        else{ //图片高度小于cell的高度、就要上下贴边、左右突出来
            
            h = self.contentView.height;
            w = h * image.size.width * 1.0 / image.size.height;
            y = 0;
            x = 0;
            
            self.imageView.frame = CGRectMake(x, y, w, h);
            
            self.imageView.center = CGPointMake(self.contentView.width*0.5, self.contentView.height*0.5);
            
//            self.scrollView.contentSize = CGSizeMake(w, h);
//
//            self.scrollView.contentInset = UIEdgeInsetsMake(100, 100, 100, 100);
//
//            self.scrollView.contentOffset = CGPointMake((w - self.contentView.width)*0.5, 0);
            
        }
        
        
    }
    else{ //cell的宽大于高
        
        CGFloat h = self.contentView.height;
        CGFloat w = h * image.size.width * 1.0 / image.size.height;
        CGFloat y = 0;
        CGFloat x = 0;
        if(w > self.contentView.width){ //图片宽度大于cell的宽度、就要上下贴边、左右突出来
            
            h = self.contentView.height;
            w = h * image.size.width * 1.0 / image.size.height;
            y = 0;
            x = 0;
            
            self.imageView.frame = CGRectMake(x, y, w, h);
            
            self.imageView.center = CGPointMake(self.contentView.width*0.5, self.contentView.height*0.5);
            
//            self.scrollView.contentSize = CGSizeMake(w, h);
//
//            self.scrollView.contentInset = UIEdgeInsetsMake(100, 100, 100, 100);
//
//            self.scrollView.contentOffset = CGPointMake((w - self.contentView.width)*0.5, 0);
            
            
        }
        else{ //图片宽度小于cell的宽度、就要左右贴边、上下突出来
            
            
            w = self.contentView.width;
            h = w * image.size.height * 1.0 / image.size.width;
            y = 0;
            x = 0;
            
            self.imageView.frame = CGRectMake(x, y, w, h);
            
            self.imageView.center = CGPointMake(self.contentView.width*0.5, self.contentView.height*0.5);
            
//            self.scrollView.contentSize = CGSizeMake(w, h);
//
//            self.scrollView.contentInset = UIEdgeInsetsMake(100, 100, 100, 100);
//
//            self.scrollView.contentOffset = CGPointMake(0, (h - self.contentView.height)*0.5);
            
            
            
        }
        
    }
    
}

//-(UIScrollView *)scrollView{
//    if (_scrollView == nil) {
//        _scrollView = [[UIScrollView alloc] init];
//        _scrollView.delegate = self;
//        _scrollView.minimumZoomScale = 0.5;
//        _scrollView.maximumZoomScale = 5.0;
//        _scrollView.showsVerticalScrollIndicator = NO;
//        _scrollView.showsHorizontalScrollIndicator = NO;
//        _scrollView.alwaysBounceVertical = YES;
//        _scrollView.alwaysBounceHorizontal = YES;
//
//    }
//    return _scrollView;
//}


-(UIImageView *)imageView{
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
        
    }
    return _imageView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.userInteractionEnabled = NO; //防止它做点击cell、点击cell它又会走layoutSubView方法、这样图片被强制还原原来的frame了、假如你真想做点击、你就在collectionView添加点击首饰、并且判断手指是否在cell内
    
    self.layer.cornerRadius = 5;
    self.clipsToBounds = YES;
    
    
//    [self.contentView addSubview:self.scrollView];
//    [self.scrollView addSubview:self.imageView];
    [self.contentView addSubview:self.imageView];
    
}

//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
//{
//    return self.imageView;
//}

-(void)setStatus:(YWPuzzleStatus *)status{
    _status = status;
    
    
    self.imageView.image = status.image;
    
}

-(void)setPanXInscrease:(CGFloat)panXInscrease{
    _panXInscrease = panXInscrease;
    
    
    self.imageView.centerX += panXInscrease;
    
    
}

-(void)setPanYInscrease:(CGFloat)panYInscrease{
    _panYInscrease = panYInscrease;
    
    self.imageView.centerY += panYInscrease;
    
}

-(void)setPanCenter:(CGPoint)panCenter{
    _panCenter = panCenter;
    self.imageView.center = panCenter;
}

-(void)setScaleInscrease:(CGFloat)scaleInscrease{
    _scaleInscrease = scaleInscrease;
    
    self.imageView.bounds = CGRectMake(0, 0, self.imageView.width * scaleInscrease, self.imageView.height * scaleInscrease);
    
}

@end
